package com.youmu.xxessj.ac

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.github.kittinunf.fuel.httpPost
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.youmu.xxessj.R
import com.youmu.xxessj.ad.MyListad
import com.youmu.xxessj.en.MyList
import com.youmu.xxessj.ut.DELETSJ
import com.youmu.xxessj.ut.SLECTSJ
import com.youmu.xxessj.ut.SLECTSSSJ
import com.youmu.xxessj.ut.SLECTSSSJZJ
import kotlinx.android.synthetic.main.activity_sjlist.*
import kotlinx.android.synthetic.main.heard.*
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.selector
import org.jetbrains.anko.singleTop
import org.jetbrains.anko.toast

class sjlistActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sjlist)
        if(intent.getStringExtra("type") == "1"){
            init_heard()
            init()
        }else{
            init_heard2()
            init2()
        }
    }

    fun init_heard(){
        heard_back.setOnClickListener {
            finish()
        }
        heard_ok.visibility = View.GONE
        heard_title.text = "浏览书籍"
    }

    fun init_heard2(){
        heard_back.setOnClickListener {
            finish()
        }
        heard_ok.visibility = View.GONE
        heard_title.text = "我的书籍"
    }

    fun init(){
        srinit()
        sjlist.layoutManager = LinearLayoutManager(this)
        SLECTSJ.httpPost()
                .responseString { request, response, result ->
                    result.fold(
                            success = {
                                if (it != "[]"){
                                    val user:List<MyList> = Gson().fromJson(it, object : TypeToken<List<MyList>>() {}.type)
                                    val adapter = MyListad(user)
                                    adapter?.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
                                    sjlist.adapter = adapter
                                    adapter.setOnItemClickListener { adapter, view, position ->
                                        startActivity(intentFor<sjdelActivity>("id" to user[position].id
                                                ,"uid" to user[position].upid
                                                ,"bookname" to user[position].bookname
                                                ,"bookcb" to user[position].bookcb
                                                ,"bookimg" to user[position].bookimg
                                                ,"bookzz" to user[position].bookzz).singleTop())
                                    }
                                }else{
                                    toast("请求错误")
                                }
                            },
                            failure = {
                                toast("请求错误")
                            }
                    )
                }
    }


    fun srinit(){
        ss_ok.setOnClickListener {
            var name =  ss_bookname.text.toString()
            SLECTSSSJ.httpPost(listOf("bookname" to name))
                    .responseString { request, response, result ->
                        result.fold(
                                success = {
                                    if (it != "[]"){
                                        val user:List<MyList> = Gson().fromJson(it, object : TypeToken<List<MyList>>() {}.type)
                                        val adapter = MyListad(user)
                                        adapter?.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
                                        sjlist.adapter = adapter
                                        adapter.setOnItemClickListener { adapter, view, position ->
                                            startActivity(intentFor<sjdelActivity>("id" to user[position].id
                                                    ,"uid" to user[position].upid
                                                    ,"bookname" to user[position].bookname
                                                    ,"bookcb" to user[position].bookcb
                                                    ,"bookimg" to user[position].bookimg
                                                    ,"bookzz" to user[position].bookzz).singleTop())
                                        }
                                    }else{
                                        toast("请求错误")
                                    }
                                },
                                failure = {
                                    toast("请求错误")
                                }
                        )
                    }
        }
    }

    fun init2(){
        ss.visibility = View.GONE
        sjlist.layoutManager = LinearLayoutManager(this)
        SLECTSSSJZJ.httpPost(listOf("uid" to intent.getStringExtra("id")))
                .responseString { request, response, result ->
                    result.fold(
                            success = {
                                if (it != "[]"){
                                    val user:List<MyList> = Gson().fromJson(it, object : TypeToken<List<MyList>>() {}.type)
                                    val adapter = MyListad(user)
                                    adapter?.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
                                    sjlist.adapter = adapter
                                    adapter.setOnItemClickListener { adapter, view, position ->
                                        selector("选择", listOf("修改","删除"), { dialogInterface, i ->
                                            if (i == 0){
                                                startActivity(intentFor<upsjActivity>("id" to user[position].id
                                                        ,"uid" to user[position].upid
                                                        ,"bookname" to user[position].bookname
                                                        ,"bookcb" to user[position].bookcb
                                                        ,"bookimg" to user[position].bookimg
                                                        ,"bookzz" to user[position].bookzz).singleTop())
                                            }else if(i ==1){
                                                DELETSJ.httpPost(listOf("id" to user[position].id)) .responseString { request, response, result ->
                                                    result.fold(
                                                            success = {
                                                                if (it == "1"){
                                                                    toast("删除成功")
                                                                }else{
                                                                    toast("请求错误")
                                                                }
                                                            },
                                                            failure = {
                                                                toast("请求错误")
                                                            }
                                                    )
                                                }

                                            }
                                        })
                                    }
                                }else{
                                    toast("请求错误")
                                }
                            },
                            failure = {
                                toast("请求错误")
                            }
                    )
                }
    }
}
